import requests
from dotenv import load_dotenv
import os

# 加载 .env 文件
load_dotenv()

def get_lunar_and_solar_terms(date):
    # 从环境变量中获取 app_id 和 app_secret
    app_id = os.getenv('MXNZP_APP_ID')
    app_secret = os.getenv('MXNZP_APP_SECRET')
    
    if not app_id or not app_secret:
        print("请确保 .env 文件中正确设置了 APP_ID 和 APP_SECRET")
        return None, None
    
    # 构建请求 URL
    url = f"https://www.mxnzp.com/api/holiday/single/{date}"
    params = {
        'app_id': app_id,
        'app_secret': app_secret
    }
    
    # 发送 GET 请求
    response = requests.get(url, params=params)
    
    # 检查响应状态
    if response.status_code == 200:
        data = response.json()
        if data['code'] == 1:
            # 获取农历和节气信息
            lunar_calendar = data['data']['lunarCalendar']
            solar_terms = data['data']['solarTerms']
            return lunar_calendar, solar_terms
        else:
            return None, None
    else:
        return None, None
